#!/usr/bin/python

# ----------------- BEGIN LICENSE BLOCK ---------------------------------
#
# Copyright (c) 2019-2021 Intel Corporation
#
# SPDX-License-Identifier: LGPL-2.1-only
#
# ----------------- END LICENSE BLOCK -----------------------------------

"""
This module uses pygccxml and py++ to generate
Python interfaces for all classes and datatypes
of the ad_rss library.

The resulting library is called libad_rss_python
"""

from python_wrapper_helper import generate_python_wrapper, post_process_python_wrapper


def main():
    """
    Main function to generate Python-C++ binding code
    """
    include_dirs = {
        "@GENPY_INCLUDE_DIRS@",
        "@CMAKE_CURRENT_LIST_DIR@/../generated/include/",
        "@CMAKE_CURRENT_LIST_DIR@/../include/"
    }

    declarations={"toString", "fromString", "std::to_string", "std::sqrt", "std::fabs", "std::numeric_limits"}

    additional_replacements = {
        # RoadSegment: vector<vector>>, set name manually, tool doesn't understand correctly
        ("\"vector_less__ad_scope_rss_scope_world_scope_LaneSegment__greater_\"", "\"RoadSegment\"")
    }

    for component in ["core", "situation", "state", "unstructured", "world"]:
        main_namespace="ad::rss::{}".format(component)

        declarations_component=declarations.copy()
        declarations_component.add(main_namespace)

        ignore_declarations={}
        additional_includes={}
        if component == "state":
            ignore_declarations={
                # alias is not matched by the namespace magic
                "ObjectIdVector"
            }

        if component == "core":
            header_dirs = {
                "@CMAKE_CURRENT_LIST_DIR@/../include/ad/rss/{}".format(component)
            }
        elif component == "world":
            header_dirs = {
                "@CMAKE_CURRENT_LIST_DIR@/../generated/include/ad/rss/{}".format(component),
            }
        elif component == "unstructured":
            header_dirs = {
                "@CMAKE_CURRENT_LIST_DIR@/../include/ad/rss/{}".format(component)
            }
        else:
            header_dirs = {
                "@CMAKE_CURRENT_LIST_DIR@/../generated/include/ad/rss/{}".format(component),
                "@CMAKE_CURRENT_LIST_DIR@/../include/ad/rss/{}".format(component)
            }

        generate_python_wrapper(library_name=component,
                                cpp_filename="AdRss-{}-PythonWrapper.cpp.in".format(component),
                                header_directories=header_dirs,
                                include_paths=include_dirs,
                                main_namespace=main_namespace,
                                declarations=declarations_component,
                                ignore_declarations=ignore_declarations)

        additional_replacements_component = additional_replacements.copy()
        additional_replacements_component.add(
                ("BOOST_PYTHON_MODULE({}){{".format(component),
                 "void export_ad_rss_{}(){{\n\n".format(component) +
                 "  bp::object ad_rss_{}_module(bp::handle<>(bp::borrowed(PyImport_AddModule(\"{}\"))));\n".format(component, component) +
                 "  bp::scope().attr(\"{}\") = ad_rss_{}_module;\n".format(component, component) +
                 "  bp::scope submodule_scope = ad_rss_{}_module;\n\n".format(component) +
                 "  bp::scope().attr(\"__doc__\") = \"Python binding of ad_rss ad::rss::{} namespace C++ code for @PYTHON_BINDING_NAME@\";".format(component) +
                 "  bp::scope().attr(\"__copyright__\") = \"Copyright (C) 2019-2021 Intel Corporation\";" +
                 "  bp::scope().attr(\"__license__\") = \"LGPL-2.1-only\";"
                ))
        post_process_python_wrapper(header_directories=header_dirs,
                                    cpp_filename_in="AdRss-{}-PythonWrapper.cpp.in".format(component),
                                    cpp_filename_out="AdRss-{}-PythonWrapper.cpp".format(component),
                                    additional_replacements=additional_replacements_component,
                                    spdx_license="LGPL-2.1-only"
        )

if __name__ == '__main__':
    main()
